package com.kll.tv.views.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.kll.carolinehmibiz.net.bean.GetPhotoAlbumResponse
import com.kll.tv.R
import com.kll.tv.databinding.ItemPhotoAlbumContentBinding
import com.kll.tv.databinding.ItemPhotoAlbumTimeBinding


/**
 * ====================================================
 * Created by Ziliang Cai on 2022/1/11 20:57
 * <a href="mailto:752577456@qq.com">Contact me</a>
 * ====================================================
 */
class PhotoSlbumLovelyAdapter(var context: Context): RecyclerView.Adapter<RecyclerView.ViewHolder>()  {
    private var mItemClickListener: OnPhotoListItemClickListener? = null
    private var isEdit = false // 是否编辑
    private var checkeds = ArrayList<Boolean>()
    private var photoPosition = 1

    // 数据
    var data: MutableList<Any> = ArrayList()
        set(value) {
            if (value == null) {
                checkeds.clear()
                data.clear()
            } else {
                field = value
                checkeds.clear()
                for (data in photoData) {
                    if (data is GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo) {
                        checkeds.add(false)
                    }
                }
            }
            notifyDataSetChanged()
        }
    var photoData: MutableList<GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo> = ArrayList()
        set(value) {
            if (value == null) {
                photoData.clear()
            } else {
                field = value
            }
        }
    var photoUrlData: MutableList<String> = ArrayList()
        set(value) {
            if (value == null) {
                photoUrlData.clear()
            } else {
                field = value
            }
        }

    fun addData(data: ArrayList<Any>) {
        if (data != null) {
            this.data.addAll(data)
            notifyDataSetChanged()
        }
    }

    /**
     * 设置是否编辑状态
     */
    fun setIsEdit(isEdit: Boolean) {
        this.isEdit = isEdit
        setAllChecked(false)
    }

    /**
     * 设置全选
     */
    fun setAllChecked(isAllChecked: Boolean) {
        for (i in checkeds.indices) {
            checkeds[i] = isAllChecked
        }
        notifyDataSetChanged()
    }

    fun isEdit(): Boolean {
        return isEdit
    }

    /**
     * 获取所有选中的ID
     */
    fun getChecksArray(): ArrayList<Int> {
        val checkIds = ArrayList<Int>()
        for (i in photoData.indices) {
            if (checkeds[i] == true) {
                checkIds.add(photoData[i].id)
            }
        }
        return checkIds
    }

    private fun refreshCheckUI(ivChoose: ImageView, isChecked: Boolean) {
        ivChoose.visibility = if (isEdit) View.VISIBLE else View.GONE
        ivChoose.setImageResource(if (isChecked) R.drawable.icon_photo_check else R.drawable.picture_checkbox_selector)
    }

    inner class TimeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding = ItemPhotoAlbumTimeBinding.bind(itemView)
    }

    inner class ContentHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding = ItemPhotoAlbumContentBinding.bind(itemView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return if (viewType == 0) {
            view = LayoutInflater.from(parent.context).inflate(R.layout.item_photo_album_time, parent, false)
            return TimeHolder(view)
        } else {
            view = LayoutInflater.from(parent.context).inflate(R.layout.item_photo_album_content, parent, false)
            return ContentHolder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val data = data.get(position)

        if (holder is TimeHolder) {
            holder.binding.tvTime.text = data.toString()
        } else if (holder is ContentHolder) {
            for (i in 0..(photoUrlData.size - 1)) {
                if (photoUrlData[i] == (data as GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo).url) {
                    photoPosition = i
                }
            }

            Glide.with(context)
                .load((data as GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo).url)
                .apply(RequestOptions().placeholder(R.drawable.icon_photo_gary))
                .into(holder.binding.ivPhoto)
            if((data as GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo).type == 1) {
                holder.binding.tvVideoTime.visibility = View.GONE
            } else {
                holder.binding.tvVideoTime.visibility = View.VISIBLE
            }

            refreshCheckUI(holder.binding.ivSelect, checkeds[photoPosition])

            if (mItemClickListener != null) {
                holder.itemView.setOnClickListener {
                    if (isEdit) {
                        for (i in 0..(photoUrlData.size - 1)) {
                            if (photoUrlData[i] == (data as GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo).url) {
                                photoPosition = i
                            }
                        }
                        checkeds[photoPosition] = !checkeds[photoPosition]
                        refreshCheckUI(holder.binding.ivSelect, checkeds[photoPosition])
                        mItemClickListener!!.onItemCheckClickListener()
                    } else {
                        mItemClickListener!!.onPhotoItemClick(data)
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (data[position] is String) {
            return 0
        } else {
            return 1
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager = recyclerView.layoutManager
        if (manager is GridLayoutManager) {
            val gridManager: GridLayoutManager? = manager as GridLayoutManager?
            gridManager!!.setSpanSizeLookup(object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return if (getItemViewType(position) == 0) gridManager.spanCount else 1
                }

            })
        }
    }

    interface OnPhotoListItemClickListener {
        fun onPhotoItemClick(data: GetPhotoAlbumResponse.PhotoAlbum.PhotoInfo)
        fun onItemCheckClickListener()
    }

    fun setOnPhotoListItemClickListener(listener: OnPhotoListItemClickListener) {
        this.mItemClickListener = listener
    }

}